package com.rika.controller;

import com.rika.pojo.*;
import com.rika.response.QueryResult;
import com.rika.response.Result;
import com.rika.service.*;
import com.rika.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SomainController {
    @Resource
    private SomainService somainService;
    @Resource
    private SoItemService soItemService;
    @Resource
    private CustomerService customerService;
    @Resource
    private ProductService productService;
    @Resource
    private StockRecordService stockRecordService;
    @Resource
    private PayRecordService payRecordService;
    @RequestMapping("getAllSomain")
    public QueryResult selectAllSomain(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<SoMain> list=somainService.selectAllSomain(params);
        int total=somainService.countAll2();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("addSomain")
    public Result addSomain(SoMain somain, String soItemLists, String customerName){
        Result result=new Result();
        List<Customer> customerCodelist=customerService.selectCustomerByName(customerName);
        String customerCode=customerCodelist.get(0).getCustomerCode();
        somain.setCustomerCode(customerCode);
        somainService.addSomain(somain);
        String[] list=soItemLists.split(":");
        String[] list3= new String[2*list.length];
        for(int i=0;i<list.length;i++){
            String[] list2=list[i].split(",");
            for(int j=0;j<list2.length;j++){
                list3[i*2+j]=list2[j];
            }
        }
        String[] list4=new String[list3.length/2];
        for(int i=0;i<list3.length;i+=2){
            list3[i]=list3[i].replace("\"","");
            list3[i]=list3[i].replace("{","");
            list3[i]=list3[i].replace("[","");
            list3[i]=list3[i].replace("}","");
            list3[i]=list3[i].replace("]","");
            list4[i/2]=list3[i];
        }
        String[] list5=new String[list4.length-1];
        for(int i=1;i<list4.length;i++){
            list5[i-1]=list4[i];
        }
        int t=list5.length;
        int s=t/6;
        while (t!=0){
            SoItem soItem=new SoItem();
            for(int n=0;n<s;n++){
                //System.out.println(list5[0+n*6]+" "+list5[1+n*6]+" "+list5[2+n*6]+" "+list5[3+n*6]+" "+list5[4+n*6]+" "+list5[5+n*6]);
                soItem.setSoId(Long.parseLong(list5[0+n*6]));
                soItem.setProductCode(list5[1+n*6]);
                soItem.setItemPrice(Float.parseFloat(list5[2+n*6]));
                soItem.setUnitName(list5[3+n*6]);
                soItem.setNum(Integer.parseInt(list5[4+n*6]));
                soItem.setUnitPrice(Float.parseFloat(list5[5+n*6]));
                t-=6;
                soItemService.addSoItem(soItem);
            }
        }
        result.setCode(200);
        result.setMsg("新增成功");
        return result;
    }
    @RequestMapping("deleteSomain")
    public Result deleteSoId(Long soId){
        Result result=new Result();
        soItemService.deleteSoItem(soId);
        somainService.deleteSoMain(soId);
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }
    @RequestMapping("selectBySoId")
    public Result selectBySoId(Long soId){
        Result result=new Result();
        List<SoMain> soMainList=somainService.selectBySoId(soId);
        List<SoItem> soItemList=soItemService.selectAllSoItemList(soId);
        soMainList.get(0).setSoItemList(soItemList);
        result.setCode(200);
        result.setData(soMainList);
        return result;
    }
    @RequestMapping("updateSomain")
    public Result updateSomain(SoMain somain,String soItemLists){
        Result result=new Result();
        somainService.updateSoMain(somain);
        soItemService.deleteSoItem(somain.getSoId());
        String[] list=soItemLists.split(":");
        String[] list3= new String[2*list.length];
        for(int i=0;i<list.length;i++){
            String[] list2=list[i].split(",");
            for(int j=0;j<list2.length;j++){
                list3[i*2+j]=list2[j];
            }
        }
        String[] list4=new String[list3.length/2];
        for(int i=0;i<list3.length;i+=2){
            list3[i]=list3[i].replace("\"","");
            list3[i]=list3[i].replace("{","");
            list3[i]=list3[i].replace("[","");
            list3[i]=list3[i].replace("}","");
            list3[i]=list3[i].replace("]","");
            list4[i/2]=list3[i];
        }
        String[] list5=new String[list4.length-1];
        for(int i=1;i<list4.length;i++){
            list5[i-1]=list4[i];
        }
        int t=list5.length;
        int s=t/6;
        while (t!=0){
            SoItem soItem=new SoItem();
            for(int n=0;n<s;n++){
                //System.out.println(list5[0+n*6]+" "+list5[1+n*6]+" "+list5[2+n*6]+" "+list5[3+n*6]+" "+list5[4+n*6]+" "+list5[5+n*6]);
                soItem.setSoId(Long.parseLong(list5[0+n*6]));
                soItem.setProductCode(list5[1+n*6]);
                soItem.setItemPrice(Float.parseFloat(list5[2+n*6]));
                soItem.setUnitName(list5[3+n*6]);
                soItem.setNum(Integer.parseInt(list5[4+n*6]));
                soItem.setUnitPrice(Float.parseFloat(list5[5+n*6]));
                t-=6;
                soItemService.addSoItem(soItem);
            }
        }
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("getAllSomainForSettle")
    public QueryResult selectAllSomainForSettle(Integer currentPage,Integer pageSize,String payType){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("payType",payType);
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<SoMain> list=somainService.selectAllSomainForSettle(params);
        int total=somainService.countAll2();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("settleSomain")
    public Result settleSomain(Long soId,String endUser){
        Result result=new Result();
        Map<String,Object> params=new HashMap<>();
        params.put("soId",soId);
        params.put("endUser",endUser);
        somainService.settleSoMain(params);
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("somainQuery")
    public Result somainQuery(String soId,String date,Integer status){
        Result result=new Result();
        Map<String,Object> params=new HashMap<>();
        String startTime="";
        String endTime="";
        if(date.length()>0){
            String[] Date=date.split("~");
            startTime=Date[0].trim();
            endTime=Date[1].trim();
        }
        params.put("soId",soId);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("status",status);
        List<SoMain> list=somainService.selectSoMainQuery(params);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("getAllSomainInStore")
    public QueryResult selectAllSomainInStore(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<SoMain> list=somainService.selectSoMainInStore(params);
        int total=somainService.selectSoMainInStoreCount();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateSoMainOutStore")
    public Result updateSoMainOutStore(Long soId,StockRecord stockRecord){
        Result result=new Result();
        Map<String,Object> params2=new HashMap<>();
        params2.put("soId",soId);
        params2.put("stockTime",stockRecord.getStockTime());
        params2.put("stockUser",stockRecord.getCreateUser());
        somainService.updateSoMainToOutStore(params2);
        List<SoItem> list=soItemService.selectAllSoItemBySoId(soId);
        for(int i=0;i<list.size();i++){
            Map<String,Object> params=new HashMap<>();
            List<Product> list2=productService.selectByProductCode(list.get(i).getProductCode());
            params.put("num",list2.get(0).getNum()-list.get(i).getNum());
            params.put("soNum",list2.get(0).getSoNum()+list.get(i).getNum());
            params.put("productCode",list.get(i).getProductCode());
            productService.updateproductOutStore(params);
            stockRecord.setProductCode(list.get(i).getProductCode());
            stockRecord.setStockNum(list.get(i).getNum());
            stockRecord.setStockType(0);
            stockRecordService.addStockRecord(stockRecord);
        }
        result.setCode(200);
        result.setMsg("出库成功");
        return result;
    }
    @RequestMapping("selectSoMainIncome1")
    public QueryResult selectSoMainIncome1(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<SoMain> list=somainService.selectSoMainIncome1(params);
        int total=somainService.selectSoMainIncome1CountAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateincome1")
    public Result updateincome1(SoMain soMain,String pay_Time,String account,String orderCode,String pay_Type){
        PayRecord payRecord=new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrderCode(orderCode);
        payRecord.setPay_Time(pay_Time);
        payRecord.setPay_Type(5);
        Result result=new Result();
        somainService.updateincome1(soMain);
        List<SoMain> list=somainService.selectBySoId(soMain.getSoId());
        payRecord.setPay_price(list.get(0).getSoTotal());
        payRecordService.addPayRecord(payRecord);
        result.setCode(200);
        result.setMsg("收款成功");
        return result;
    }
    @RequestMapping("selectSoMainIncome2")
    public QueryResult selectSoMainIncome2(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<SoMain> list=somainService.selectSoMainIncome2(params);
        int total=somainService.selectSoMainIncome2CountAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateincome2")
    public Result updateincome2(SoMain soMain,String pay_Time,String account,String orderCode){
        PayRecord payRecord=new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrderCode(orderCode);
        payRecord.setPay_Time(pay_Time);
        payRecord.setPay_Type(6);
        Result result=new Result();
        somainService.updateincome1(soMain);
        List<SoMain> list=somainService.selectBySoId(soMain.getSoId());
        payRecord.setPay_price(list.get(0).getSoTotal());
        payRecordService.addPayRecord(payRecord);
        result.setCode(200);
        result.setMsg("收款成功");
        return result;
    }
    @RequestMapping("selectSoMainIncome3")
    public QueryResult selectSoMainIncome3(Integer currentPage, Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<SoMain> list=somainService.selectSoMainIncome3(params);
        int total=somainService.selectSoMainIncome3CountAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("updateincome3")
    public Result updateincome3(SoMain soMain,String pay_Time,String account,String orderCode){
        PayRecord payRecord=new PayRecord();
        payRecord.setAccount(account);
        payRecord.setOrderCode(orderCode);
        payRecord.setPay_Time(pay_Time);
        List<SoMain> list=somainService.selectBySoId(soMain.getSoId());
        //预付
        if(list.get(0).getStatus()==1){
            payRecord.setPay_Type(7);
            payRecord.setPay_price(list.get(0).getPrePayFee());
            somainService.updateincome2(soMain);
        }
        //尾款
        if(list.get(0).getStatus()==2){
            payRecord.setPay_Type(8);
            payRecord.setPay_price(list.get(0).getSoTotal()-list.get(0).getPrePayFee());
            somainService.updateincome1(soMain);
        }
        Result result=new Result();
        payRecordService.addPayRecord(payRecord);
        result.setCode(200);
        result.setMsg("收款成功");
        return result;
    }
    @RequestMapping("selectSoMainNum")
    public Result selectSoMainNum(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<SoMain> somainNum=somainService.selectSoMainNum(params);
        result.setCode(200);
        result.setData(somainNum);
        return result;
    }
    @RequestMapping("selectSoMainEndNum")
    public Result selectSoMainEndNum(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        Integer soMainEndNum=somainService.selectSoMainEndNum(params);
        result.setCode(200);
        result.setData(soMainEndNum);
        return result;
    }
    @RequestMapping("selectEndSomainPrice")
    public Result selectEndSomainPrice(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<SoMain> endSomainPrice=somainService.selectEndSomainPrice(params);
        result.setCode(200);
        result.setData(endSomainPrice);
        return result;
    }
    @RequestMapping("selectSoMainReportList")
    public Result selectSoMainReportList(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<SoMain> soMainReportList=somainService.selectSoMainReportList(params);
        result.setCode(200);
        result.setData(soMainReportList);
        return result;
    }
}
